package kd.comt.plugin;

import java.util.HashMap;
import java.util.Map;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.base.BaseShowParameter;
import kd.bos.bill.BillShowParameter;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.tree.TreeNode;
import kd.bos.form.ClientProperties;
import kd.bos.form.ShowType;
import kd.bos.form.container.Tab;
import kd.bos.form.control.TreeView;
import kd.bos.form.control.events.TabSelectEvent;
import kd.bos.form.control.events.TabSelectListener;
import kd.bos.form.control.events.TreeNodeClickListener;
import kd.bos.form.control.events.TreeNodeEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListFilterParameter;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;

public class SX_cs extends AbstractFormPlugin implements TreeNodeClickListener, TabSelectListener{
    /**
	 * 树节点点击事件
	 */
	@Override
	public void treeNodeClick(TreeNodeEvent evt) {
		TreeNodeClickListener.super.treeNodeClick(evt);
		String nodeId = evt.getNodeId().toString();
		ListShowParameter showParameter = new ListShowParameter();
		showParameter.getOpenStyle().setShowType(ShowType.InContainer);
		if (StringUtils.equals("0-1-1", nodeId)) {// 单据1暂存态数据
			showParameter.getOpenStyle().setTargetKey("kdec_flex_billlist");
			showParameter.setBillFormId("kdec_demobillfortab");
			ListFilterParameter listFilterParameter = new ListFilterParameter();
			listFilterParameter.setFilter(new QFilter("billstatus", QCP.equals, "A"));// 暂存
			showParameter.setListFilterParameter(listFilterParameter);
		} else if (StringUtils.equals("0-1-2", nodeId)) {// 单据1提交态数据
			showParameter.getOpenStyle().setTargetKey("kdec_flex_billlist");
			showParameter.setBillFormId("kdec_demobillfortab");
			ListFilterParameter listFilterParameter = new ListFilterParameter();
			listFilterParameter.setFilter(new QFilter("billstatus", QCP.equals, "B"));// 提交
			showParameter.setListFilterParameter(listFilterParameter);
		} else if (StringUtils.equals("0-1-3", nodeId)) {// 单据1审核态数据
			showParameter.getOpenStyle().setTargetKey("kdec_flex_billlist");
			showParameter.setBillFormId("kdec_demobillfortab");
			ListFilterParameter listFilterParameter = new ListFilterParameter();
			listFilterParameter.setFilter(new QFilter("billstatus", QCP.equals, "C"));// 审核
			showParameter.setListFilterParameter(listFilterParameter);
		} else if (StringUtils.equals("0-2-1", nodeId)) {// 基礎資料
			showParameter.getOpenStyle().setTargetKey("kdec_flex_baselist");
			showParameter.setBillFormId("kdec_basefortab");
			ListFilterParameter listFilterParameter = new ListFilterParameter();
			listFilterParameter.setFilter(new QFilter("status", QCP.equals, "A"));// 暫存
			showParameter.setListFilterParameter(listFilterParameter);
			Tab tab = this.getView().getControl("kdec_tabap");
			tab.activeTab("kdec_tabpageap2");
		}
		this.getView().showForm(showParameter);
	}

	/**
	 * 构建树形节点
	 */
	private void treeviewLoad() {
		final String rootId = "0"; // 根节点id
		TreeView treeView = this.getControl("kdec_treeviewap");
		treeView.setCustomeStyles("fs:55");

		Map<String, Object> ctrl = new HashMap<String, Object>();
		ctrl.put(ClientProperties.FontSize, "18");
		this.getView().updateControlMetadata("kdec_treeviewap", ctrl);

		TreeNode rootNode = new TreeNode(null, rootId, "全部单据树根节点", true);
		rootNode.setIsOpened(true);
		treeView.setRootVisible(false);
		TreeNode tn1 = new TreeNode(rootId, "0-1", "案例单据1", true);
		tn1.setIsOpened(true);
		TreeNode tn2 = new TreeNode(rootId, "0-2", "案例单据2", true);
		TreeNode tn3 = new TreeNode(rootId, "0-3", "其他", true);
		// 单据1
		TreeNode tn11 = new TreeNode("0-1", "0-1-1", "案例单据1(暂存)", false);
		TreeNode tn12 = new TreeNode("0-1", "0-1-2", "案例单据1(提交)", false);
		TreeNode tn13 = new TreeNode("0-1", "0-1-3", "案例单据1(审核)", false);
		tn1.addChild(tn11);
		tn1.addChild(tn12);
		tn1.addChild(tn13);
		// 单据2
		TreeNode tn21 = new TreeNode("0-2", "0-2-1", "基礎資料(暂存)", false);
		tn2.addChild(tn21);
		// 其他
		TreeNode tn31 = new TreeNode("0-3", "0-3-1", "其他(1)", false);
		TreeNode tn32 = new TreeNode("0-3", "0-3-2", "其他(2)", false);
		tn3.addChild(tn31);
		tn3.addChild(tn32);
		// 添加到根菜单
		rootNode.addChild(tn1);
		rootNode.addChild(tn2);
		rootNode.addChild(tn3);
		treeView.addNode(rootNode);

	}

	@Override
	public void tabSelected(TabSelectEvent arg0) {
		String tabKey = arg0.getTabKey();
		if (StringUtils.equals(tabKey, "kdec_tabpageap")) {// 切换页签-单据列表
			ListShowParameter showParameter = new ListShowParameter();
			showParameter.setBillFormId("kdec_demobillfortab");
			showParameter.getOpenStyle().setShowType(ShowType.InContainer);
			showParameter.getOpenStyle().setTargetKey("kdec_flex_billlist");
			this.getView().showForm(showParameter);
		} else if (StringUtils.equals(tabKey, "kdec_tabpageap1")) {// 切换页签-单据
			BillShowParameter showParameter = new BillShowParameter();
			showParameter.setFormId("kdec_demobillfortab");
			showParameter.getOpenStyle().setShowType(ShowType.InContainer);
			showParameter.getOpenStyle().setTargetKey("kdec_flex_bill");
			this.getView().showForm(showParameter);
		} else if (StringUtils.equals(tabKey, "kdec_tabpageap2")) {// 切换页签-基础资料列表
			ListShowParameter showParameter = new ListShowParameter();
			showParameter.setBillFormId("kdec_basefortab");
			showParameter.getOpenStyle().setShowType(ShowType.InContainer);
			showParameter.getOpenStyle().setTargetKey("kdec_flex_baselist");
			this.getView().showForm(showParameter);
		} else if (StringUtils.equals(tabKey, "kdec_tabpageap3")) {// 切换页签-基础资料
			BaseShowParameter showParameter = new BaseShowParameter();
			showParameter.setFormId("kdec_basefortab");
			showParameter.getOpenStyle().setShowType(ShowType.InContainer);
			showParameter.getOpenStyle().setTargetKey("kdec_flex_base");
			this.getView().showForm(showParameter);
		}
	}


}
